<?php
/**
 * Created by 784855684@qq.com.
 * Link: https://github.com/lichtung/wshore
 * User: linzh
 * Date: 6/30/17
 * Time: 2:57 PM
 */
declare(strict_types=1);


namespace application\module\admin\addon;


use application\module\admin\addon\menu\model\MenuModel;
use wshore\library\TreeSort;
use wshore\throws\DatabaseException;
use wshore\throws\db\RecordNotFoundException;

/**
 * Class Menu
 *
 * @method Menu getInstance(...$params) static
 *
 * @package application\module\admin\addon
 */
class Menu extends Addon
{
    protected function getName(): string
    {
        return 'menu';
    }

    protected function models(): array
    {
        return [MenuModel::class];
    }

    /**
     * 获取格式化后的菜单列表
     * @param bool $nested 是否嵌套
     * @return array
     * @throws DatabaseException 数据库查询出错时抛出
     */
    public function getlist($nested = true): array
    {
        $model = new MenuModel();
        $list = $model->getlist();
        $list = (new TreeSort($list))->tolist($nested);
        return $list;
    }

    /**
     * 删除菜单项
     * @param int $id 菜单项ID
     * @return array 返回删除
     * @throws RecordNotFoundException 记录不存在
     * @throws DatabaseException 记录删除失败
     */
    public function delete(int $id): array
    {
        $menu = new MenuModel($id);
        $menu->disable = true;
        if ($menu->save()) {
            return $menu->all();
        } else {
            throw new DatabaseException($menu->error());
        }
    }

    /**
     * 修改菜单项
     * @param int $id
     * @param string $name
     * @param string $value
     * @param int $pid
     * @param string $icon
     * @param int $category
     * @param int $orderNo
     * @param string $target
     * @return array 返回更新后的数据
     * @throws DatabaseException
     * @throws RecordNotFoundException
     */
    public function update(int $id, string $name, string $value, int $pid = 0, string $icon = 'circle-thin', int $category = 0, int $orderNo = 0, string $target = '_self'): array
    {
        $menu = new MenuModel($id);
        $menu->name = $name;
        $menu->value = $value;
        $menu->pid = $pid;
        $menu->icon = $icon;
        $menu->category = $category;
        $menu->orderNo = $orderNo;
        $menu->target = $target;

        if ($menu->save()) {
            return $menu->all();
        } else {
            throw new DatabaseException($menu->error());
        }
    }

    /**
     * 添加菜单项
     * @param string $name
     * @param string $value
     * @param int $pid
     * @param string $icon
     * @param int $category
     * @param int $orderNo
     * @param string $target
     * @return array 返回更新后的数据
     * @throws DatabaseException
     */
    public function add(string $name, string $value, int $pid = 0, string $icon = 'circle-thin', int $category = 0, int $orderNo = 0, string $target = '_self'): array
    {
        $menu = new MenuModel();
        $menu->name = $name;
        $menu->pid = $pid;
        $menu->category = $category;
        $menu->icon = $icon;
        $menu->value = $value;
        $menu->orderNo = $orderNo;
        $menu->target = $target;
        if ($menu->save()) {
            return $menu->all();
        } else {
            throw new DatabaseException($menu->error());
        }
    }
}